#include "ServerWindow.h"

/*--------------------------------------------------------------------------------------------
--
--	FUNCTION: ServerWindow
--
--	Date: March 24, 2010
--
--	REVISIONS (Date and Description):
--
--	DESIGNER: Erick Ribeiro
--
--	PROGRAMMER: Erick Ribeiro
--
--  INTERFACE: ServerWindow::ServerWindow (QWidget * parent) : QMainWindow(parent),
--															   serverIsRunning(false)
--
--      Observable * observable: The object to notify when an event occurs.
--      int eventCode: What event occured.
--
--  RETURNS: N/A
--
--	NOTES: Creates the program server GUI and initializes the Playlist and Multicasting 
--         servers.
--
--------------------------------------------------------------------------------------------*/
ServerWindow::ServerWindow (QWidget * parent) : QMainWindow(parent),
                                                serverIsRunning(false)
{
    GlobalError::initInstance();

    playlistServer = new PlaylistServer();
    streamServer = new MulticastServer(playlistServer->getPlaylist());

    setWindowTitle("SocialRadio Server");
    setCentralWidget(new QWidget());
    resize(400, 400);

    initMenu();
    updateMenu();

    initMainLayout();
    updateGUI();
}

/*--------------------------------------------------------------------------------------------
--
--	FUNCTION: startServer
--
--	Date: March 24, 2010
--
--	REVISIONS (Date and Description):
--
--	DESIGNER: Erick Ribeiro
--
--	PROGRAMMER: Erick Ribeiro
--
--  INTERFACE: void ServerWindow::startServer ()
--
--  RETURNS: void
--
--	NOTES: Starts the Playlist and Multicasting servers.
--
--------------------------------------------------------------------------------------------*/
void ServerWindow::startServer ()
{
    playlistServer->runServer();
    streamServer->runServer();

    serverIsRunning = true;
    updateGUI();
}

/*--------------------------------------------------------------------------------------------
--
--	FUNCTION: addSong
--
--	Date: March 24, 2010
--
--	REVISIONS (Date and Description):
--
--	DESIGNER: Erick Ribeiro
--
--	PROGRAMMER: Erick Ribeiro
--
--  INTERFACE: void ServerWindow::addSong ()
--
--  RETURNS: void
--
--	NOTES: Opens up the file browsing dialog box when the user decides to upload a song
--         onto the playlist.
--
--------------------------------------------------------------------------------------------*/
void ServerWindow::addSong ()
{
    QString path = QFileDialog::getOpenFileName(this,
                                                    tr("Choose audio file"),
                                                    "~",
                                                    tr("Audio (*.wav *.mp3)"));

    if (path.size() > 1)
    {
        path.replace(QString("/"), QString("\\"));
		std::string filename = extractFilename(path.toStdString());
        Song * song = new Song(filename, path.toStdString());
        playlistServer->getPlaylist()->addSong(song);
    }
}

/*--------------------------------------------------------------------------------------------
--
--	FUNCTION: micToggled
--
--	Date: March 24, 2010
--
--	REVISIONS (Date and Description):
--
--	DESIGNER: Erick Ribeiro
--
--	PROGRAMMER: Erick Ribeiro
--
--  INTERFACE: void ServerWindow::micToggled ()
--
--  RETURNS: void
--
--	NOTES: Activates microphone mode and disables the streaming mode on the GUI.
--
--------------------------------------------------------------------------------------------*/
void ServerWindow::micToggled ()
{
    if (micAction->isChecked())
    {
        playlistAction->setChecked(false);
        streamServer->toggleMic(true);
    }
    else
    {
        streamServer->stopRunning();
    }
}

/*--------------------------------------------------------------------------------------------
--
--	FUNCTION: playlistToggled
--
--	Date: March 24, 2010
--
--	REVISIONS (Date and Description):
--
--	DESIGNER: Erick Ribeiro
--
--	PROGRAMMER: Erick Ribeiro
--
--  INTERFACE: void ServerWindow::playlistToggled ()
--
--  RETURNS: void
--
--	NOTES: Activates playlist mode and disables the microphone mode on the GUI.
--
--------------------------------------------------------------------------------------------*/
void ServerWindow::playlistToggled ()
{
    if (playlistAction->isChecked())
    {
        micAction->setChecked(false);
        streamServer->toggleMic(false);
    }
    else
    {
        streamServer->stopRunning();
    }
}

/*--------------------------------------------------------------------------------------------
--
--	FUNCTION: initMenu
--
--	Date: March 24, 2010
--
--	REVISIONS (Date and Description):
--
--	DESIGNER: Erick Ribeiro
--
--	PROGRAMMER: Erick Ribeiro
--
--  INTERFACE: void ServerWindow::initMenu ()
--
--  RETURNS: void
--
--	NOTES: Various menu items on the program's server GUI.
--
--------------------------------------------------------------------------------------------*/
void ServerWindow::initMenu ()
{
    serverMenu = menuBar()->addMenu("&Server");

    startAction = new QAction(tr("&Start Server"), this);
    startAction->setShortcut(tr("CTRL+S"));
    startAction->setIcon(QIcon(":/Resources/Images/Start.png"));
    serverMenu->addAction(startAction);

    quitAction = new QAction(tr("&Quit"), this);
    quitAction->setShortcut(tr("CTRL+Q"));
    serverMenu->addAction(quitAction);

    mediaMenu = menuBar()->addMenu("&Media");

    addSongAction = new QAction(tr("&Add Song..."), this);
    addSongAction->setIcon(QIcon(":/Resources/Images/Media.png"));
    mediaMenu->addAction(addSongAction);

    QObject::connect(startAction, SIGNAL(triggered()), this, SLOT(startServer()));
    QObject::connect(quitAction, SIGNAL(triggered()), this, SLOT(quit()));
    QObject::connect(addSongAction, SIGNAL(triggered()), this, SLOT(addSong()));
}

/*--------------------------------------------------------------------------------------------
--
--	FUNCTION: updateMenu
--
--	Date: March 24, 2010
--
--	REVISIONS (Date and Description):
--
--	DESIGNER: Erick Ribeiro
--
--	PROGRAMMER: Erick Ribeiro
--
--  INTERFACE: void ServerWindow::updateMenu ()
--
--  RETURNS: void
--
--	NOTES: 
--
--------------------------------------------------------------------------------------------*/
void ServerWindow::updateMenu ()
{
    if (serverIsRunning)
    {
        startAction->setDisabled(true);
    }
    else
    {
        startAction->setEnabled(true);
    }
}

/*--------------------------------------------------------------------------------------------
--
--	FUNCTION: initMainLayout
--
--	Date: March 24, 2010
--
--	REVISIONS (Date and Description):
--
--	DESIGNER: Erick Ribeiro
--
--	PROGRAMMER: Erick Ribeiro
--
--  INTERFACE: void ServerWindow::initMainLayout ()
--
--  RETURNS: void
--
--	NOTES: Sets out the layout of the program server's GUI such as buttons, logo, initial
--         sizing, etc.
--
--------------------------------------------------------------------------------------------*/
void ServerWindow::initMainLayout ()
{
    toolBar = addToolBar(tr("Streaming"));

    micAction = toolBar->addAction("Microphone");
    micAction->setIcon(QIcon(":/Resources/Images/Microphone.png"));
    micAction->setCheckable(true);

    playlistAction = toolBar->addAction("Playlist");
    playlistAction->setIcon(QIcon(":/Resources/Images/Music.png"));
    playlistAction->setCheckable(true);

    QObject::connect(micAction, SIGNAL(triggered()), this, SLOT(micToggled()));
    QObject::connect(playlistAction, SIGNAL(triggered()), this, SLOT(playlistToggled()));

    mainLayout = new QVBoxLayout();

    playlistWidget = new PlaylistWidget(playlistServer->getPlaylist());
    playlistWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Maximum);
    mainLayout->addWidget(playlistWidget);

    mainLayout->addStretch();

    centralWidget()->setLayout(mainLayout);

	logo = new QPixmap();
	logo->load(":Resources/Images/socialradio.png", "PNG");
	imageLabel = new QLabel(0, this);
	imageLabel->setPixmap(*logo);
	mainLayout->addWidget(imageLabel, Qt::AlignHCenter);
}

/*--------------------------------------------------------------------------------------------
--
--	FUNCTION: updateGUI
--
--	Date: March 24, 2010
--
--	REVISIONS (Date and Description):
--
--	DESIGNER: Erick Ribeiro
--
--	PROGRAMMER: Erick Ribeiro
--
--  INTERFACE: void ServerWindow::updateGUI ()
--
--  RETURNS: void
--
--	NOTES: 
--
--------------------------------------------------------------------------------------------*/
void ServerWindow::updateGUI ()
{
}

/*--------------------------------------------------------------------------------------------
--
--	FUNCTION: extractFilename
--
--	Date: March 24, 2010
--
--	REVISIONS (Date and Description):
--
--	DESIGNER: Erick Ribeiro
--
--	PROGRAMMER: Erick Ribeiro
--
--  INTERFACE: std::string ServerWindow::extractFilename(std::string path)
--
--      std::string path: The location of the file as a string.
--
--  RETURNS: void
--
--	NOTES: Gets the name of the data file from its string location.
--
--------------------------------------------------------------------------------------------*/
std::string ServerWindow::extractFilename(std::string path)
{
    unsigned int position;

	//find the last occurance of backslash
	position = path.rfind("\\");
	if (position != std::string::npos)
	{
		//get everything after final backslash (filename)
        return path.substr(position+1);
	}
	return path;
}